package service.user.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import common.entities.Page;
import dao.permission.PermissionDao;
import dao.user.UserManageDao;
import entities.login.User;
import entities.permission.Permission;
import entities.permission.Role;
import entities.permission.UserAndRole;
import service.user.UserManageService;
import util.base.CurrentUser;
@Service
public class UserMangeServiceImpl implements UserManageService {
	
	@Autowired
    private  UserManageDao userMangeDao;
	@Autowired
	private PermissionDao permissionDao;
	
	@Override
	public Page<User> getUsers(String name,int page, int rows) {
		Page<User> pageUser = new Page<User>();
		List<User> userlist=new ArrayList<User>();
		if(!"".equals(name)&&null!=name){
			name="%"+name+"%";
		}
		int total=userMangeDao.getUserCount(name);
		int offset=(page-1)*rows;
		userlist=userMangeDao.getUsers(name,offset,rows);
		for(int i=0;i<userlist.size();i++){
			String userId=userlist.get(i).getId();
			List<Role> roles=permissionDao.getRolesOfUser(userId);
			userlist.get(i).setRoles(roles);
		}
		pageUser.setTotal(total);
		pageUser.setItems(userlist);
		return pageUser;
	}
	@Override
	public boolean getUser(String id,String name) {
		User user=userMangeDao.getUser(id,name);
		if(user==null){
			return false;
		}
		return true;
	}
	@Override
	public void addUser(User user) {
		
	   if(user.getRoles().size()!=0){
		  List<UserAndRole> userAndRoles=new ArrayList<UserAndRole>();
		  for(Role role:user.getRoles()){
			  UserAndRole userAndRole=new UserAndRole();
			  userAndRole.setId(UUID.randomUUID().toString());
			  userAndRole.setAdd_person(CurrentUser.getCurrentUser().getName());
			  userAndRole.setAdd_time(new Date());
			  userAndRole.setUserId(user.getId());
			  userAndRole.setRoleId(role.getId());
			  userAndRoles.add(userAndRole);
		  }
		  permissionDao.addUserAndRole(userAndRoles); 
	   }
	   userMangeDao.addUser(user);
	}
	@Override
	public void updateUser(User user) {
		//获得更新用户的角色信息
		List<UserAndRole> olduserAndRoles=permissionDao.getUserAndRole(user.getId());
		 List<String> noExistuserAndRoleId=new ArrayList<String>();
		 List<UserAndRole> userAndRoles=new ArrayList<UserAndRole>();
		//获得删除的用户和角色关系
		for(UserAndRole userAndRole:olduserAndRoles){
			    boolean noExist=true;
				for(Role role:user.getRoles()){
					if(userAndRole.getRoleId()==role.getId()){
						noExist=false;
						break;
					}
				}
				if(noExist){
					noExistuserAndRoleId.add(userAndRole.getId());
				}
		}
		//获得新增角色关系
		for(Role role:user.getRoles()){
			boolean newRole=true;
			for(UserAndRole userAndRole:olduserAndRoles){
				if(role.getId()==userAndRole.getRoleId()){
					newRole=false;
					break;
				}
			}
			if(newRole){
				  UserAndRole userAndRole=new UserAndRole();
				  userAndRole.setId(UUID.randomUUID().toString());
				  userAndRole.setAdd_person(CurrentUser.getCurrentUser().getName());
				  userAndRole.setAdd_time(new Date());
				  userAndRole.setUserId(user.getId());
				  userAndRole.setRoleId(role.getId());
				  userAndRoles.add(userAndRole);
			}
		}
		 if(noExistuserAndRoleId.size()!=0){
			 permissionDao.deleteUserAndRoles(noExistuserAndRoleId);
		 }
		 if(userAndRoles.size()!=0){
			 permissionDao.addUserAndRole(userAndRoles); 
		 }
		 userMangeDao.updateUser(user);
	}
	@Override
	public void deleteUser(String ids) {
		List<String> idList=Arrays.asList(ids.split(","));
		userMangeDao.delete(idList);
	}
	@Override
	public User getUserPermissions(String userName) {
		User user=userMangeDao.getUser(null,userName);
		if(user!=null){
			   List<Role> roles=userMangeDao.getRoles(user.getId());
			   for(int i=0;i<roles.size();i++){
					List<Permission> permissions=userMangeDao.getPermissionAndMenus(roles.get(i).getId());
					roles.get(i).setPermissions(permissions);
				}
			user.setRoles(roles);
		}
		return user;
	}
	
	
}
